package com.xckj.ibox.view;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.TextPaint;
import android.text.method.LinkMovementMethod;
import android.text.style.ClickableSpan;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.StyleRes;

import com.xckj.ibox.R;

/**
 * @Author: ChenYao
 * @Describe: 自定义Dialog
 * @Date: 2023/6/16 23:09
 */
public class CustomDialog extends Dialog {
    private ImageView ivDisagree;
    private ImageView ivAgree;
    private TextView tvMessage;
    private OnCancelClickListener cancelClickListener;
    private OnSureClickListener sureClickListener;
    private OnLookAgreementClickListenerOne lookAgreementClickListenerOne;
    private OnLookAgreementClickListenerTwo lookAgreementClickListenerTwo;

    public CustomDialog(@NonNull Context context, @StyleRes int themeResId) {
        super(context, themeResId);
    }

    public void setCancelClickListener(OnCancelClickListener cancelClickListener) {
        this.cancelClickListener = cancelClickListener;
    }

    public void setSureOnclickListener(OnSureClickListener sureClickListener) {
        this.sureClickListener = sureClickListener;
    }

    public void setLookAgreementClickListenerOne(OnLookAgreementClickListenerOne lookAgreementClickListenerOne) {
        this.lookAgreementClickListenerOne = lookAgreementClickListenerOne;
    }

    public void setLookAgreementClickListenerTwo(OnLookAgreementClickListenerTwo lookAgreementClickListenerTwo) {
        this.lookAgreementClickListenerTwo = lookAgreementClickListenerTwo;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.custom_dialog_view);
        setCanceledOnTouchOutside(false);
        setCancelable(false);
        initView();
        initDialogHint();
        setListener();
    }


    private void initView() {
        ivDisagree = findViewById(R.id.iv_disagree);
        ivAgree = findViewById(R.id.iv_agree);
        tvMessage = findViewById(R.id.tv_message);
    }

    private void initDialogHint() {
        String tips1 = getContext().getString(R.string.privacy_3);
        String tips2 = getContext().getString(R.string.privacy_1);
        String tips = getContext().getString(R.string.dialog_hint_2, tips1, tips2);
        SpannableStringBuilder stringBuilder = new SpannableStringBuilder(tips);
        int color = Color.parseColor("#B462FF");
        int index = tips.indexOf(tips1);
        stringBuilder.setSpan(new ClickableSpan() {
            @Override
            public void onClick(@NonNull View view) {
                if (null != lookAgreementClickListenerOne) {
                    lookAgreementClickListenerOne.onLookAgreementClickListener();
                }
            }

            @Override
            public void updateDrawState(@NonNull TextPaint ds) {
                ds.setColor(color);
            }
        }, index, index + tips1.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        index = tips.indexOf(tips2);
        stringBuilder.setSpan(new ClickableSpan() {
            @Override
            public void onClick(@NonNull View widget) {
                if (null != lookAgreementClickListenerTwo) {
                    lookAgreementClickListenerTwo.onLookAgreementClickListener();
                }
            }

            @Override
            public void updateDrawState(@NonNull TextPaint ds) {
                ds.setColor(color);
            }
        }, index, index + tips2.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        tvMessage.setText(stringBuilder);
        tvMessage.setMovementMethod(LinkMovementMethod.getInstance());
    }

    private void setListener() {
        ivDisagree.setOnClickListener(view -> {
            if (null != cancelClickListener) {
                cancelClickListener.onCancelClick();
            }
        });
        ivAgree.setOnClickListener(view -> {
            if (null != sureClickListener) {
                sureClickListener.onSureClick();
            }
        });
    }

    /**
     * 取消按钮接口
     */
    public interface OnCancelClickListener {
        /**
         * 取消
         */
        public void onCancelClick();
    }

    /**
     * 确定按钮接口
     */
    public interface OnSureClickListener {
        /**
         * 确定
         */
        public void onSureClick();
    }

    public interface OnLookAgreementClickListenerOne {
        /**
         * 查看协议
         */
        public void onLookAgreementClickListener();
    }

    public interface OnLookAgreementClickListenerTwo {
        /**
         * 查看协议
         */
        public void onLookAgreementClickListener();
    }
}
